package com.ysd.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.modules.sys.dao.OptionsSysDao;
import com.ysd.modules.sys.entity.Options;
import com.ysd.modules.sys.model.TopicResp;
import com.ysd.modules.sys.service.OptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dapeng
 * @since 2021-07-15
 */
@Service
public class OptionsServiceImpl extends ServiceImpl<OptionsSysDao, Options> implements OptionsService {

	@Autowired
	OptionsSysDao optionsSysDao;

	@Override
	public List<TopicResp> listSearchByTopicIds(List<TopicResp> records) {
		List<Integer> ids = records.stream().map(e -> e.getId()).collect(Collectors.toList());

		QueryWrapper wrapper = new QueryWrapper();
		wrapper.in("topic_id", ids);
		List<Options> list = this.baseMapper.selectList(wrapper);

		for (TopicResp topic : records) {
			for (Options option : list) {
				if (topic.getId() == option.getTopicId()) {
					String answerId = topic.getAnswerId();
					String[] split = answerId.split(",");
					for (String trueIds : split) {
						if (Integer.parseInt(trueIds) == option.getId()) {
							if ("".equals(topic.getTrueAnswer())) {
								topic.setTrueAnswer(option.getOptions());
							} else {
								topic.setTrueAnswer(topic.getTrueAnswer() + " " + option.getOptions());
							}
						}
					}

					switch (option.getOptions()) {
						case "A":
							topic.setA(option.getContent());
							break;
						case "B":
							topic.setB(option.getContent());
							break;
						case "C":
							topic.setC(option.getContent());
							break;
						case "D":
							topic.setD(option.getContent());
							break;
					}
				}
			}
		}

		return records;
	}

	
}
